package core.manager
{
import core.Language;
import core.model.GameModel;
import core.model.PlayerModel;

import flash.errors.IllegalOperationError;

import mx.utils.StringUtil;

import values.common.CostValue;
import values.item.IItemValue;
import values.item.INumbericItemValue;

import view.component.message.Message;

/**
 * CostManager
 * @author jliang
 *
 */
public class CostManager
{
	private static function get playerModel():PlayerModel
	{
		return GameModel.instance.playerModel;
	}
	
	public static function hasEnoughToCost(cost:CostValue, showMessage:Boolean = true):Boolean
	{
		var list:Vector.<IItemValue> = cost.costValueList;
		var isEnough:Boolean = true;
		
		for each (var item:IItemValue in list)
		{
			if (item is INumbericItemValue)
			{
				if (!playerModel.hasEnoughNumbericItem(INumbericItemValue(item)))
				{
					isEnough = false;
					break;
				}
			}
			else
			{
				if ( playerModel.getItemNum(item.key, item.params) <= 0)
				{
					isEnough = false;
					break;
				}
			}
		}
		
		if (!isEnough && showMessage)
		{
			Message.show(StringUtil.substitute(Language.getText("@message_not_enough_to_cost"), item.template.name));
		}
		
		return isEnough;
	}
	
	public static function cost(value:CostValue):Vector.<IItemValue>
	{
		var list:Vector.<IItemValue> = value.costValueList;
		for each (var item:IItemValue in list)
		{
			if (item is INumbericItemValue)
			{
				playerModel.removeItem(item);
				
				trace("扣除item: " + item.key + "  x " + INumbericItemValue(item).num + "       剩余: " + playerModel.getItemNum(item.key));
			}
			else
			{
				throw new IllegalOperationError("还没实现非资源类item扣除功能");
			}
		}
        return list;
	}
}
}







